package cn.wolfcode.utils;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {
    private final static String EMPLOYEE_IN_SESSION="EMPLOYEE_IN_SESSION";
    private final static String EXPRESSION_IN_SESSION="EXPRESSION_IN_SESSION";

    public static HttpSession getSession(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = attributes.getRequest().getSession();
        return session;
    }

    /**
     * 存入用户
     * @param employee
     */
    public static void setCurrentUser(Employee employee){
        HttpSession session = UserContext.getSession();
        session.setAttribute("EMPLOYEE_IN_SESSION", employee);
    }

    /**
     * 取出用户
     * @return
     */
    public static Employee getCurrentUser(){
        HttpSession session = UserContext.getSession();
        Employee employee= (Employee) session.getAttribute("EMPLOYEE_IN_SESSION");
        return employee;
    }

    /**
     * 存入权限
     * @param list
     */
    public static void setExpressions(List list){
        HttpSession session = UserContext.getSession();
        session.setAttribute("EXPRESSION_IN_SESSION",list);
    }

    /**
     * 取出权限
     * @return
     */
    public static List getExpressions(){
        HttpSession session = UserContext.getSession();
        List list= (List) session.getAttribute("EXPRESSION_IN_SESSION");
        return list;
    }
}
